CRC16R_TABLE = [] * 256

def crc16_ibm_r(data, crc):
    crc16ibm = 0xA001;          # 0x8005
    
    crc ^= data;
    for i in range(0, 8, 1):
        if (crc & 1):
            crc >>= 1
            crc ^= crc16ibm
        else:
            crc >>= 1
    return crc


def CRC16R_Table_Init():
    for i in range(0, 256, 1):
        new_item = crc16_ibm_r(i, 0)
        CRC16R_TABLE.append(new_item)

    print(CRC16R_TABLE)    
    print(len(CRC16R_TABLE))

def calc_crc16(data, crc):
    c = crc & 0xff
    crc >>= 8

    in_list_data = data ^ c
    crc = crc ^ CRC16R_TABLE[in_list_data]
    return crc

def get_crc16():
    item = 0
    temp = 0
    data = []

    lenth = int(input("input data len:"))

    for i in range(0, lenth, 1):
        input_data = input("input first data:")
        data.append(int(input_data, 16))

    print(data)

    for crc_item in range(0, lenth, 1):
        temp = calc_crc16(data[crc_item], item)
        item = temp

    temp = hex(temp)
    print(f"crc16_res: {temp}")

    